package com.nytd.demo.controller;

import com.nytd.demo.constant.Constants;
import com.nytd.demo.entity.UserInfo;
import com.nytd.demo.service.UserInfoService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RequestMapping("/user")
@RestController
public class UserController {
    @Autowired
    private UserInfoService userInfoService;

    @RequestMapping("/login")
    public Boolean login(String name, String password, HttpSession session){
        log.info("name:" + name);
        if (!StringUtils.hasLength(name) || !StringUtils.hasLength(password)) {
            return false;
        }
        UserInfo userInfo = userInfoService.selectByName(name);
        if (userInfo == null) {
            return false;
        }
        if (password.equals(userInfo.getPassword())) {
            session.setAttribute(Constants.SESSION_USER_NAME, name);
            return true;
        }
        return false;
    }
}
